home *** CD-ROM | disk | FTP | other *** search
/ Enter 2007 April / ENTER_CD_04_07.iso / Internet / WinHTTrack 3.23 / httrack-3.23.exe / {app} / src / htscatchurl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-08  |  7.5 KB  |  279 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: URL catch .h                                           */
  34. /* Author: Xavier Roche                                         */
  35. /* ------------------------------------------------------------ */
  36.  
  37. // Fichier intercepteur d'URL .c
  38.  
  39. /* specific definitions */
  40. /* specific definitions */
  41. #include "htsbase.h"
  42. #include "htsnet.h"
  43. #include "htslib.h"
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <time.h>
  48. #include <fcntl.h>
  49. #if HTS_WIN
  50. #else
  51. #include <arpa/inet.h>
  52. #endif
  53. /* END specific definitions */
  54.  
  55. /* dΘfinitions globales */
  56. #include "htsglobal.h"
  57.  
  58. /* htslib */
  59. /*#include "htslib.h"*/
  60.  
  61. /* catch url */
  62. #include "htscatchurl.h"
  63.  
  64.  
  65. // URL Link catcher
  66.  
  67. // 0- Init the URL catcher with standard port
  68.  
  69. // catch_url_init(&port,&return_host);
  70. HTSEXT_API T_SOC catch_url_init_std(int* port_prox,char* adr_prox) {
  71.   T_SOC soc;
  72.   int try_to_listen_to[]={8080,3128,80,81,82,8081,3129,31337,0,-1};
  73.   int i=0;
  74.   do {
  75.     soc=catch_url_init(&try_to_listen_to[i],adr_prox);
  76.     *port_prox=try_to_listen_to[i];
  77.     i++;
  78.   } while( (soc == INVALID_SOCKET) && (try_to_listen_to[i]>=0));
  79.   return soc;
  80. }
  81.  
  82.  
  83. // 1- Init the URL catcher
  84.  
  85. // catch_url_init(&port,&return_host);
  86. HTSEXT_API T_SOC catch_url_init(int* port,char* adr) {
  87.   T_SOC soc = INVALID_SOCKET;
  88.   char h_loc[256+2];
  89.  
  90.   if (gethostname(h_loc,256)==0) {    // host name
  91.     SOCaddr server;
  92.     int server_size=sizeof(server);
  93.     t_hostent* hp_loc;
  94.     t_fullhostent buffer;
  95.  
  96.     // effacer structure
  97.     memset(&server, 0, sizeof(server));
  98.     
  99.     if ( (hp_loc=vxgethostbyname(h_loc, &buffer)) ) {  // notre host      
  100.  
  101.       // copie adresse
  102.       SOCaddr_copyaddr(server, server_size, hp_loc->h_addr_list[0], hp_loc->h_length);
  103.  
  104.       if ( (soc=socket(SOCaddr_sinfamily(server), SOCK_STREAM, 0)) != INVALID_SOCKET) {
  105.         SOCaddr_initport(server, *port);
  106.         if ( bind(soc,(struct sockaddr*) &server,server_size) == 0 ) {
  107.           SOCaddr server2;
  108.           int len;
  109.           len=sizeof(server2);
  110.           // effacer structure
  111.           memset(&server2, 0, sizeof(server2));
  112.           if (getsockname(soc,(struct sockaddr*) &server2,&len) == 0) {
  113.             *port=ntohs(SOCaddr_sinport(server));  // rΘcupΘrer port
  114.             if (listen(soc,10)>=0) {    // au pif le 10
  115.               SOCaddr_inetntoa(adr, 128, server2, len);
  116.             } else {
  117. #if _WIN32
  118.               closesocket(soc);
  119. #else
  120.               close(soc);
  121. #endif
  122.               soc=INVALID_SOCKET;
  123.             }
  124.             
  125.             
  126.           } else {
  127. #if _WIN32
  128.             closesocket(soc);
  129. #else
  130.             close(soc);
  131. #endif
  132.             soc=INVALID_SOCKET;
  133.           }
  134.           
  135.           
  136.         } else {
  137. #if _WIN32
  138.           closesocket(soc);
  139. #else
  140.           close(soc);
  141. #endif
  142.           soc=INVALID_SOCKET;
  143.         }
  144.       }
  145.     }
  146.   }
  147.   return soc;
  148. }
  149.  
  150. // 2 - Wait for URL
  151.  
  152. // catch_url
  153. // returns 0 if error
  154. // url: buffer where URL must be stored - or ip:port in case of failure
  155. // data: 32Kb
  156. HTSEXT_API int catch_url(T_SOC soc,char* url,char* method,char* data) {
  157.   int retour=0;
  158.  
  159.   // connexion (accept)
  160.   if (soc != INVALID_SOCKET) {
  161.     T_SOC soc2;
  162.     struct sockaddr dummyaddr;
  163.     int dummylen = sizeof(struct sockaddr);  
  164.     while ( (soc2=accept(soc,&dummyaddr,&dummylen)) == INVALID_SOCKET);
  165.   /*
  166. #ifdef _WIN32
  167.     closesocket(soc);
  168. #else
  169.     close(soc);
  170. #endif
  171.   */
  172.     soc = soc2;
  173.     /* INFOS */
  174.     {
  175.       SOCaddr server2;
  176.       int len;
  177.       len=sizeof(server2);
  178.       // effacer structure
  179.       memset(&server2, 0, sizeof(server2));
  180.       if (getpeername(soc,(struct sockaddr*) &server2,&len) == 0) {
  181.         char dot[256+2];
  182.         SOCaddr_inetntoa(dot, 256, server2, sizeof(server2));
  183.         sprintf(url,"%s:%d", dot, htons(SOCaddr_sinport(server2)));  
  184.       }
  185.     }
  186.     /* INFOS */
  187.  
  188.     // rΘception
  189.     if (soc != INVALID_SOCKET) {
  190.       char line[1000];
  191.       char protocol[256];
  192.       line[0]=protocol[0]='\0';
  193.       //
  194.       socinput(soc,line,1000);
  195.       if (strnotempty(line)) {
  196.         if (sscanf(line,"%s %s %s",method,url,protocol) == 3) {
  197.           char url_adr[HTS_URLMAXSIZE*2];
  198.           char url_fil[HTS_URLMAXSIZE*2];
  199.           // mΘthode en majuscule
  200.           int i,r=0;
  201.           url_adr[0]=url_fil[0]='\0';
  202.           //
  203.           for(i=0;i<(int) strlen(method);i++) {
  204.             if ((method[i]>='a') && (method[i]<='z'))
  205.               method[i]-=('a'-'A');
  206.           }
  207.           // adresse du lien
  208.           if (ident_url_absolute(url,url_adr,url_fil)>=0) {
  209.             // Traitement des en-tΩtes
  210.             char loc[HTS_URLMAXSIZE*2];
  211.             htsblk blkretour;
  212.             memset(&blkretour, 0, sizeof(htsblk));    // effacer
  213.             blkretour.location=loc;    // si non nul, contiendra l'adresse vΘritable en cas de moved xx
  214.             // Lire en tΩtes restants
  215.             sprintf(data,"%s %s %s\r\n",method,url_fil,protocol);
  216.             while(strnotempty(line)) {
  217.               socinput(soc,line,1000);
  218.               treathead(NULL,NULL,NULL,&blkretour,line);  // traiter
  219.               strcatbuff(data,line);
  220.               strcatbuff(data,"\r\n");
  221.             }
  222.             // CR/LF final de l'en tΩte inutile car dΘja placΘ via la ligne vide juste au dessus
  223.             //strcatbuff(data,"\r\n");
  224.             if (blkretour.totalsize>0) {
  225.               int len=(int)min(blkretour.totalsize,32000);
  226.               int pos=strlen(data);
  227.               // Copier le reste (post Θventuel)
  228.               while((len>0) && ((r=recv(soc,(char*) data+pos,len,0))>0) ) {
  229.                 pos+=r;
  230.                 len-=r;
  231.                 data[pos]='\0';       // terminer par NULL
  232.               }
  233.             }
  234.             // Envoyer page
  235.             sprintf(line,CATCH_RESPONSE);
  236.             send(soc,line,strlen(line),0);
  237.             // OK!
  238.             retour=1;
  239.           }
  240.         }
  241.       }  // sinon erreur
  242.     }
  243.   }
  244.   if (soc != INVALID_SOCKET) {
  245. #ifdef _WIN32
  246.     closesocket(soc);
  247.     /*
  248.     WSACleanup();
  249.     */
  250. #else
  251.     close(soc);
  252. #endif
  253.   }
  254.   return retour;
  255. }
  256.  
  257.  
  258.  
  259. // Lecture de ligne sur socket
  260. void socinput(T_SOC soc,char* s,int max) {
  261.   int c;
  262.   int j=0;
  263.   do {
  264.     unsigned char b;
  265.     if (recv(soc,(char*) &b,1,0)==1) {
  266.       c=b;
  267.       switch(c) {
  268.         case 13: break;  // sauter CR
  269.         case 10: c=-1; break;
  270.         case 9: case 12: break;  // sauter ces caractΦres
  271.         default: s[j++]=(char) c; break;
  272.       }
  273.     } else
  274.       c=EOF;
  275.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  276.   s[j++]='\0';
  277. }
  278.  
  279.